package com.tech.arno.spicysdk.okhttp.request;


import android.util.Log;

import java.io.File;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;


/*
*   @package:     com.tech.arno.arnospicysdk.okhttp.request
*   @fileName:    CommonRequest
*   @author:    ArnoFrost
*   @date:  2017/12/7 0007 10:32
*   @desc:     接受请求参数, 为我们生成Request对象
*/
public class CommonRequest {

    /**
     * create the key-value Request
     *
     * @param url
     * @param params
     * @return 返回一个创建好的Request对象
     */
    public static Request createPostRequest(String url, RequestParams params) {
        //1. 建立FormBody
        FormBody.Builder mFormBodyBuild = new FormBody.Builder();

        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                //2. 将请求参数遍历添加到我们的请求构件类中
                mFormBodyBuild.add(entry.getKey(), entry.getValue());
            }
            //打印
            Log.d("CommonRequest_URL", "createPostRequest: " + url);
            Log.d("CommonRequest", "createPostRequest: " + params.toString());
        }

        //3. 通过请求构件类的build方法获取到真正的请求体对象
        FormBody mFormBody = mFormBodyBuild.build();

        Request text = new Request.Builder().url(url)
                .post(mFormBody).build();
        return text;
    }

    /**
     * ressemble the params to the url
     *
     * @param url
     * @param params
     * @return 通过传入的参数 返回一个Get类型的请求
     */
    public static Request createGetRequest(String url, RequestParams params) {
        //1. 使用StringBuilder效率更高
        StringBuilder urlBuilder = new StringBuilder(url).append("?");
        if (params != null) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                //2. 字符串拼接
                urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            //打印
            Log.d("CommonRequest_URL", "createGetRequest: " + url);
            Log.d("CommonRequest_PARAMS", "createGetRequest: " + params.toString());
            Log.d("CommonRequest_URL_SPEC", "createGetRequest: " + urlBuilder.toString());
        }


        //3. (最后会多一个'&'所以去掉) 通过字符串获取到请求体对象
        return new Request.Builder().url(urlBuilder.substring(0, urlBuilder.length() - 1))
                .get().build();
    }

    /**
     * @param url
     * @param params
     * @return
     */
    public static Request createMonitorRequest(String url, RequestParams params) {
        StringBuilder urlBuilder = new StringBuilder(url).append("&");
        if (params != null && params.hasParams()) {
            for (Map.Entry<String, String> entry : params.urlParams.entrySet()) {
                urlBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        return new Request.Builder().url(urlBuilder.substring(0, urlBuilder.length() - 1)).get().build();
    }

    /**
     * 文件上传请求
     *
     * @return
     */
    private static final MediaType FILE_TYPE = MediaType.parse("application/octet-stream");

    public static Request createMultiPostRequest(String url, RequestParams params) {

        MultipartBody.Builder requestBody = new MultipartBody.Builder();
        requestBody.setType(MultipartBody.FORM);
        if (params != null) {

            for (Map.Entry<String, Object> entry : params.fileParams.entrySet()) {
                if (entry.getValue() instanceof File) {
                    requestBody.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""),
                            RequestBody.create(FILE_TYPE, (File) entry.getValue()));
                } else if (entry.getValue() instanceof String) {

                    requestBody.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""),
                            RequestBody.create(null, (String) entry.getValue()));
                }
            }
        }
        return new Request.Builder().url(url).post(requestBody.build()).build();
    }
}